package com.example.foodiemoments

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.foodiemoments.databinding.ActivityLoginBinding

class LoginActivity : AppCompatActivity() {

    private lateinit var binding: ActivityLoginBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupViews()
    }

    private fun setupViews() {
        // 使用本地Logo图片
        loadLogo()

        // 登录按钮点击事件
        binding.btnLogin.setOnClickListener {
            val username = binding.etUsername.text.toString().trim()
            val password = binding.etPassword.text.toString().trim()

            // 清除之前的错误提示
            binding.tilUsername.error = null
            binding.tilPassword.error = null

            // 验证输入
            when {
                username.isEmpty() -> {
                    binding.tilUsername.error = "请输入用户名"
                }
                password.isEmpty() -> {
                    binding.tilPassword.error = "请输入密码"
                }
                else -> {
                    performLogin(username, password, binding.cbRememberMe.isChecked)
                }
            }
        }

        // 注册按钮点击事件
        binding.btnRegister.setOnClickListener {
            Toast.makeText(this, "跳转到注册页面", Toast.LENGTH_SHORT).show()
            // 跳转到注册Activity
            startActivity(Intent(this, RegisterActivity::class.java))
        }

        // 忘记密码点击事件
        binding.tvForgotPassword.setOnClickListener {
            Toast.makeText(this, "跳转到忘记密码页面", Toast.LENGTH_SHORT).show()
            // 跳转到忘记密码Activity
        }
    }

    private fun loadLogo() {
        // 使用本地矢量图标
        binding.ivLogo.setImageResource(R.drawable.ic_foodie_logo)

        // 如果用的是PNG图片，替换为：
        // binding.ivLogo.setImageResource(R.drawable.logo_foodie)
    }

    private fun performLogin(username: String, password: String, rememberMe: Boolean) {
        // 这里实现您的登录逻辑
        Toast.makeText(
            this,
            "欢迎回来，$username！\n开启您的美食之旅 🍜",
            Toast.LENGTH_LONG
        ).show()

        // 示例：登录成功后跳转到主页
         val intent = Intent(this, MainActivity::class.java)
         startActivity(intent)
         finish()
    }
}
